﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace CFR.Silverlight.Core
{
    public static class UIElementExtender
    {
        public static void HookChildrenToEvent(this UIElement instance, RoutedEvent e, Delegate d, bool IncludeHandled)
        {
            instance.AddHandler(e, d, IncludeHandled);

            List<DependencyObject> kids = instance.GetAllChildren(true);

            foreach (DependencyObject kid in kids)
            {
                if (kid is UIElement)
                {
                    (kid as UIElement).AddHandler(e, d, IncludeHandled);

                }
            }
        }

        public static void UnHookChildrenFromEvent(this UIElement instance, RoutedEvent e, Delegate d)
        {
            instance.RemoveHandler(e, d);

            List<DependencyObject> kids = instance.GetAllChildren(true);

            foreach (DependencyObject kid in kids)
            {
                if (kid is UIElement)
                {
                    (kid as UIElement).RemoveHandler(e, d);

                }
            }
        }
    }
}
